package com.l2jserver.gameserver.communitybbs.Manager.Casino.BlackJack;

import java.text.DecimalFormat;
import java.util.StringTokenizer;

import com.l2jserver.Config;
import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.communitybbs.Model.BaseBBSManager;
import com.l2jserver.gameserver.datatables.ItemTable;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.items.L2Item;
import com.l2jserver.gameserver.network.clientpackets.Say2;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;

/**
 * @author fissban
 */
public class BlackJackBBSManager extends BaseBBSManager
{
	private NewGame BlackJack;
	private int apuesta = 1;
	
	private final String DOBLAR = "<td><button value=\"Double Bet\" action=\"bypass _bbsblack;doblar\" back=L2UI_CT1.button_df fore=L2UI_CT1.button_df WIDTH=100 HEIGHT=32></td>";
	private final String PEDIR = "<td><button value=\"Ask\" action=\"bypass _bbsblack;pedir\" back=L2UI_CT1.button_df fore=L2UI_CT1.button_df WIDTH=100 HEIGHT=32></td>";
	private final String PLANTAR = "<td><button value=\"NoMore\" action=\"bypass _bbsblack;plantar\" back=L2UI_CT1.button_df fore=L2UI_CT1.button_df WIDTH=100 HEIGHT=32></td>";
	private final String NUEVO = "<td><button value=\"New Game\" action=\"bypass _bbsblack;iniciar;" + apuesta + "\" back=L2UI_CT1.button_df fore=L2UI_CT1.button_df WIDTH=100 HEIGHT=32></td>";
	
	@Override
	public void parseCmd(String command, L2PcInstance activeChar)
	{
		if (command.equals("_bbsblack"))
		{
			String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/CommunityBoard/custom/casino/BlackJack/index.htm");
			
			content = content.replaceAll("%player%", activeChar.getName());
			
			final L2Item item = ItemTable.getInstance().getTemplate(Config.ITEM_ID_CASINO_BLACK);
			content = content.replaceAll("%icon%", item.getIcon());
			content = content.replaceAll("%name%", item.getName());
			
			DecimalFormat df = new DecimalFormat("###,###,###,###");
			content = content.replaceAll("%creditos%", String.valueOf(df.format(activeChar.getInventory().getInventoryItemCount(Config.ITEM_ID_CASINO_BLACK, -1))));
			content = content.replaceAll("%MinMax%", Config.BET_BLACK_1 + " / " + Config.BET_BLACK_2);
			
			content = content.replaceAll("%val1%", Config.BET_BLACK_1 + " " + item.getName());
			content = content.replaceAll("%val2%", Config.BET_BLACK_2 + " " + item.getName());
			
			separateAndSend(content, activeChar);
		}
		else if (command.startsWith("_bbsblack;"))
		{
			String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/CommunityBoard/custom/casino/BlackJack/game.htm");
			
			content = content.replaceAll("%player%", activeChar.getName());
			
			final L2Item item = ItemTable.getInstance().getTemplate(Config.ITEM_ID_CASINO_BLACK);
			content = content.replaceAll("%icon%", item.getIcon());
			content = content.replaceAll("%name%", item.getName());
			
			DecimalFormat df = new DecimalFormat("###,###,###,###");
			content = content.replaceAll("%creditos%", String.valueOf(df.format(activeChar.getInventory().getInventoryItemCount(Config.ITEM_ID_CASINO_BLACK, -1))));
			content = content.replaceAll("%MinMax%", Config.BET_BLACK_1 + " / " + Config.BET_BLACK_2);
			
			StringTokenizer st = new StringTokenizer(command, ";");
			
			st.nextToken();// command _bbsblack
			
			switch (st.nextToken())
			{
				case "iniciar":
					switch (st.nextToken())
					{
						case "1":
							apuesta = Config.BET_BLACK_1;
							break;
						case "2":
							apuesta = Config.BET_BLACK_2;
							break;
					}
					
					if (activeChar.getInventory().getInventoryItemCount(Config.ITEM_ID_CASINO_BLACK, 0) < apuesta)
					{
						activeChar.sendPacket(new CreatureSay(0, Say2.TELL, "[BlackJack]", "You need " + apuesta + "of" + Config.ITEM_ID_CASINO_BLACK + "!"));
						
						content = content.replaceAll("%sumPlayer%", "0");
						content = content.replaceAll("%cards_player%", "<td></td>");
						
						BlackJack.repartirCarta("banca");
						
						content = content.replaceAll("%sumBanca%", "0");
						content = content.replaceAll("%cards_banca%", "<td></td>");
						content = content.replaceAll("%menu%", "<td></td>");
						separateAndSend(content, activeChar);
						return;
					}
					
					BlackJack = new NewGame(activeChar, apuesta);
					
					activeChar.destroyItemByItemId("casino jugada", Config.ITEM_ID_CASINO_BLACK, BlackJack.getApuesta(), activeChar, false);
					BlackJack.repartirCarta("player");
					BlackJack.repartirCarta("player");
					
					if (BlackJack.getBlackJackPlayer())
					{
						separateAndSend(content, activeChar);
						waitTime(1000);
						parseCmd("_bbsblack;plantar", activeChar);
						return;
					}
					content = content.replaceAll("%sumPlayer%", String.valueOf(BlackJack.getSumaCartasPlayer()));
					content = content.replaceAll("%cards_player%", BlackJack.getCartasFormatHtmlPlayer());
					
					BlackJack.repartirCarta("banca");
					
					content = content.replaceAll("%sumBanca%", String.valueOf(BlackJack.getSumaCartasBanca()));
					content = content.replaceAll("%cards_banca%", BlackJack.getCartasFormatHtmlBanca());
					content = content.replaceAll("%menu%", PEDIR + PLANTAR + (BlackJack.getCountCartsPlayer() == 2 ? DOBLAR : "") + NUEVO);
					separateAndSend(content, activeChar);
					break;
				case "pedir":
					BlackJack.repartirCarta("player");
					
					if ((BlackJack.getCountCartsPlayer() == 2) && (BlackJack.getCountCartsPlayer() <= 11))
					{
						content = content.replaceAll("%sumPlayer%", String.valueOf(BlackJack.getSumaCartasPlayer()));
						content = content.replaceAll("%cards_player%", BlackJack.getCartasFormatHtmlPlayer());
						content = content.replaceAll("%sumBanca%", String.valueOf(BlackJack.getSumaCartasBanca()));
						content = content.replaceAll("%cards_banca%", BlackJack.getCartasFormatHtmlBanca());
						content = content.replaceAll("%menu%", PEDIR + DOBLAR + PLANTAR + NUEVO);
					}
					else if (BlackJack.getSumaCartasPlayer() > 21)
					{
						activeChar.sendPacket(new CreatureSay(0, Say2.TELL, "[BlackJack]", "LOSE!"));
						
						// TODO se podria sacar los primero 4 items fuera de la condicion y asi no lo repitimos
						content = content.replaceAll("%sumPlayer%", String.valueOf(BlackJack.getSumaCartasPlayer()));
						content = content.replaceAll("%cards_player%", BlackJack.getCartasFormatHtmlPlayer());
						content = content.replaceAll("%sumBanca%", String.valueOf(BlackJack.getSumaCartasBanca()));
						content = content.replaceAll("%cards_banca%", BlackJack.getCartasFormatHtmlBanca());
						content = content.replaceAll("%menu%", NUEVO);
					}
					else
					{
						content = content.replaceAll("%sumPlayer%", String.valueOf(BlackJack.getSumaCartasPlayer()));
						content = content.replaceAll("%cards_player%", BlackJack.getCartasFormatHtmlPlayer());
						content = content.replaceAll("%sumBanca%", String.valueOf(BlackJack.getSumaCartasBanca()));
						content = content.replaceAll("%cards_banca%", BlackJack.getCartasFormatHtmlBanca());
						content = content.replaceAll("%menu%", PEDIR + PLANTAR + NUEVO);
					}
					separateAndSend(content, activeChar);
					break;
				case "plantar":
					if (BlackJack.getSumaCartasBanca() < 17)
					{
						BlackJack.repartirCarta("banca");
						
						content = content.replaceAll("%sumBanca%", String.valueOf(BlackJack.getSumaCartasBanca()));
						content = content.replaceAll("%cards_banca%", BlackJack.getCartasFormatHtmlBanca());
						if (BlackJack.getBlackJackPlayer())
						{
							content = content.replaceAll("%sumPlayer%", "BlackJack");
						}
						else
						{
							content = content.replaceAll("%sumPlayer%", String.valueOf(BlackJack.getSumaCartasPlayer()));
						}
						content = content.replaceAll("%cards_player%", BlackJack.getCartasFormatHtmlPlayer());
						content = content.replaceAll("%menu%", "<td></td>");
						// content = content.replaceAll("%menu%", NUEVO);
						separateAndSend(content, activeChar);
						waitTime(1000);
						parseCmd("_bbsblack;plantar", activeChar);
					}
					else
					{
						BlackJack.winner();
						
						if (BlackJack.getBlackJackBanca())
						{
							content = content.replaceAll("%sumBanca%", "BlackJack");
						}
						else
						{
							content = content.replaceAll("%sumBanca%", String.valueOf(BlackJack.getSumaCartasBanca()));
						}
						content = content.replaceAll("%cards_banca%", BlackJack.getCartasFormatHtmlBanca());
						if (BlackJack.getBlackJackPlayer())
						{
							content = content.replaceAll("%sumPlayer%", "BlackJack");
						}
						else
						{
							content = content.replaceAll("%sumPlayer%", String.valueOf(BlackJack.getSumaCartasPlayer()));
						}
						content = content.replaceAll("%cards_player%", BlackJack.getCartasFormatHtmlPlayer());
						content = content.replaceAll("%menu%", NUEVO);
						separateAndSend(content, activeChar);
					}
					break;
				case "doblar":
					activeChar.destroyItemByItemId("casino jugada", Config.ITEM_ID_CASINO_BLACK, BlackJack.getApuesta(), activeChar, false);
					BlackJack.repartirCarta("player");
					BlackJack.setDoble(true);
					parseCmd("_bbsblack;plantar", activeChar);
					break;
			}
		}
	}
	
	/**
	 * Hacemos q se detenga todo por un determinado tiempo
	 * @param time (milisegundos)
	 */
	private void waitTime(long time)
	{
		try
		{
			Thread.sleep(time);
		}
		catch (InterruptedException ie)
		{
			ie.printStackTrace();
		}
	}
	
	@Override
	public void parseWrite(String ar1, String ar2, String ar3, String ar4, String ar5, L2PcInstance activeChar)
	{
		//
	}
	
	public static BlackJackBBSManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private static class SingletonHolder
	{
		protected static final BlackJackBBSManager _instance = new BlackJackBBSManager();
	}
}